package com.lin.fengxiu.api.v1;

import com.lin.fengxiu.core.interceptors.ScopeLevel;
import com.lin.fengxiu.dto.TokenDto;
import com.lin.fengxiu.dto.TokenGetDTO;
import com.lin.fengxiu.exception.http.NotFoundException;
import com.lin.fengxiu.service.WxAuthenticationService;
import com.lin.fengxiu.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName TokenController
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/18 21:36
 * @Version 1.0
 */
@RequestMapping(value = "token")
@RestController
public class TokenController {

    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    @PostMapping("")
    public Map<String, String> getToken(@RequestBody @Validated TokenGetDTO userData){
        Map<String ,String> map = new HashMap<>();
        String token = null;
//        LoginType.USER_WX
        switch (userData.getType()){
            case USER_WX:
                // 传入微信返回的code码，使用该 code 码去微信服务器验证获取令牌JWT
                token = wxAuthenticationService.code2Session(userData.getAccount());
                break;
            case USER_Email:

                break;
            default:
                throw new NotFoundException(10003);
        }
        map.put("token", token);
        return map;
    }

    /**
     * @Description TODO 轻量级接口，只用于对Token进行校验；上面的“” 接口会进行查询DB等繁琐操作；当请求量太高时会被微信拒绝访问，需要规避该问题
     * @param token
     * @return
     */
    @PostMapping("/verify")
    public Map<String, Boolean> verify(@RequestBody TokenDto token){
        Map<String,Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(token.getToken());
        map.put("is_valid",valid);
        return map;
    }
}
